home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ZED3DSRC.ZIP / DRAWPOLY.H < prev    next >
C/C++ Source or Header  |  1995-04-22  |  5KB  |  141 lines

  1. #ifndef __POLYDRAW_H
  2. #define __POLYDRAW_H
  3.  
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7.  
  8.  
  9. /***********************************************************************
  10.  *
  11.  * Polygon drawing routines
  12.  * (C) 1995 by Sébastien Loisel - All Rights Reserved
  13.  *
  14.  ***********************************************************************/
  15.  
  16.  
  17. /* These routines need a few special macros to work, which are defined
  18.    hereafter. These macros convert from outbuffer space to projection
  19.    plane space and vice-versa. The following macros are general purpose.
  20.    The catch is this: if you're certain that the outbuffer is of a specific,
  21.    power-of-two width, then you could replace the expensive mul/divs by
  22.    shifts, and speed up things a bit. These are used extensively, once
  23.    per scanline per edge. Anywhere these macros are used, the outbuffer is
  24.    available as out, a pointer to an outbuffer. Assume fixed point as
  25.    mentionned above. Note that the buffer should span -1 through +1 in
  26.    projection plane coords */
  27.  
  28. #define bufx2proj(x) (((x)<<8)/out->maxx)
  29. #define bufy2proj(y) (((y)<<8)/out->maxy)
  30. #define proj2bufx(x) (((x)*out->maxx)>>8)
  31. #define proj2bufy(y) (((y)*out->maxy)>>8)
  32.  
  33. /* the next one is similar to the above, except it's meant to multiply
  34.    by buffer width */
  35. #define mulwidth(y) ((y)*out->width)
  36.  
  37. /* Define here the type used for pixels */
  38. typedef unsigned char pixel;
  39.  
  40. struct edge_struct;
  41. typedef struct edge_struct edge;
  42.  
  43.  
  44. struct edge_struct
  45.     {
  46.     long x1,y1;
  47.  
  48.     /* The rest is for internal use only */
  49.     long x2,y2;
  50.     edge *next;
  51.     long intercept; /* current x or y intercept */
  52.     long numerator,denominator; /* fraction part of slope */
  53.     long increment; /* integer part of slope */
  54.     long run; /* running total of slope */
  55.     };
  56.  
  57.  
  58.  
  59.  
  60.  
  61. struct polygon_struct
  62.     {
  63.     /* The following is initialized by the caller, not the library */
  64.     long numedges; /* number of edges in array */
  65.     edge *edgetable; /* edge ARRAY!!! not list */
  66.     long A,B,C,D; /* plane equation parameters Ax+By+Cz=D */
  67.     long color; /* color for flat shading duh huh */
  68.  
  69.     /* The following is used internally by the library */
  70.     edge inactive; /* inactive edge table */
  71.     edge active; /* active edge table */
  72.     long nextremove; /* next scanline at which we'll remove edges from active */
  73.     };
  74.     /* special note:
  75.         The edge table should list all of the polygon's points as
  76.         x1 and y1 entries, in a continuous fashion. That is,
  77.         using a loop of the form for(a=0;a<numedges;a++) and looking
  78.         at edgetable[a].x1 and edgetable[a].y1 will take you around the
  79.         polygon. The entries x2 and y2 will be initialized internally later,
  80.         you do not need to initialize them */
  81.  
  82. typedef struct polygon_struct polygon;
  83.  
  84.  
  85. struct outbuffer_struct
  86.     {
  87.     long width, height; /* buffer width and height */
  88.     long maxx,maxy; /* Maximum coordinate value in absolute value. That is,
  89.                        the buffer coordinates are in [-maxx,maxx] and
  90.                        [-maxy,maxy]. */
  91.  
  92.     pixel *buffer; /* pointer to linear buffer */
  93.     };
  94.  
  95. typedef struct outbuffer_struct outbuffer;
  96.  
  97.  
  98. void drawpolygon(polygon *poly, outbuffer *out);
  99. /* draws poly on output buffer out */
  100.  
  101. outbuffer *allocbuf(long maxy, long maxx);
  102. /* allocates & initializes an outbuffer. returns 0 on error, otherwise
  103.    returns initialized outbuffer (pointer to). The buffer allocated has
  104.    a width of (maxx*2)+2 (+1 for origin, +1 so it comes to a multiple of 2).
  105.    Height is left untouched per se, but an extra line is allocated so the
  106.    global allocation is a multiple of 4 elements. */
  107.  
  108. void destroybuf(outbuffer *out);
  109. /* deallocates outbuffer and members buffer and zbuffer, overwriting them
  110.    with null pointers, hoping to ensure a crash if out is used afterwards */
  111.  
  112. /* Internal functions follow */
  113. /* Functions preceded by a h_ are meant to be used in a horizontal scan-line
  114.    algorithm, while those precede by a v_ are meant to be used in a vertical
  115.    scan-line algo, those without a prefix are invariable (can be used in
  116.    either algo) */
  117.  
  118. void init_edges(polygon *poly, outbuffer *out);
  119.     /* Calculates edge data, but don't sort yet, also inits poly->nextremove */
  120.  
  121. void sort_edges(polygon *poly, outbuffer *out);
  122.     /* Builds inactive edge table, properly sorted - if inactive->next is
  123.        0, then no polygon should be drawn */
  124.  
  125. void update_lists(polygon *poly, long scanline);
  126.     /* Updates active & inactive lists for scanline - if after calling this,
  127.        poly->active.next is zero, we're done drawing the polygon */
  128.  
  129. void update_intercept(polygon *poly);
  130.     /* Update intercept value for edges in active list */
  131.  
  132. void draw_spans(polygon *poly, outbuffer *out, long delta);
  133.     /* Draws all spans to outbuffer */
  134.  
  135.  
  136. #ifdef __cplusplus
  137. }
  138. #endif
  139.  
  140.  
  141. #endif